package com.YCH.article.service.impl;

import com.YCH.article.service.AppInformationService;
import com.YCH.model.article.dtos.AppRecommendTopicDto;
import com.YCH.model.article.pojos.TbAppRealTimeInfo;
import com.YCH.model.article.pojos.TbAppTopics;
import com.YCH.model.article.pojos.TbArticle;
import com.YCH.model.article.vos.AppRealTimeInfoId;
import com.YCH.model.article.vos.ArticleMonthlyHotList;
import com.YCH.model.common.dtos.ResponseResult;
import com.YCH.model.mappers.app.*;
import com.YCH.model.user.pojos.TbUser;
import com.YCH.model.user.pojos.TbUserFollow;
import com.YCH.model.user.vo.ApUserRecommand;
import com.YCH.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@SuppressWarnings("all")
public class AppInformationServiceImpl implements AppInformationService {
    @Autowired
    private TbAppRealTimeInfoMapper tbAppRealTimeInfoMapper;
    @Autowired
    private TbAppTopicMapper tbAppTopicMapper;
    @Autowired
    private TbUserMapper tbUserMapper;
    @Autowired
    private TbShowBehaviorMapper tbShowBehaviorMapper;
    @Autowired
    private TbArticleMapper tbArticleMapper;
    @Autowired
    private TbUserFanMapper tbUserFanMapper;
    @Autowired
    private TbUserFollowMapper tbUserFollowMapper;

    /**
     * 加载咨询
     *
     * @param list
     * @return
     */
    @Override
    public ResponseResult loadRealTimeInfo(List<AppRealTimeInfoId> list) {
        List<TbAppRealTimeInfo> tbAppRealTimeInfos = tbAppRealTimeInfoMapper.selectRealTimeInfoByFlag(list);
        return ResponseResult.okResult(tbAppRealTimeInfos);
    }

    /**
     * 加载推荐话题
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadRecommendTopic(AppRecommendTopicDto dto) {
        List<TbAppTopics> tbAppTopics = tbAppTopicMapper.selectHeighLikeTopic(dto);
        return ResponseResult.okResult(tbAppTopics);
    }

    /**
     * 加载全部话题
     *
     * @return
     */
    @Override
    public ResponseResult loadAllTopics() {
        List<TbAppTopics> tbUserLikeTopics = tbAppTopicMapper.selectAll();
        System.out.println(tbUserLikeTopics);
        System.out.println("******************************");
        return ResponseResult.okResult(tbUserLikeTopics);
    }

    /**
     * 推荐用户
     *
     * @return
     */
    @Override
    public ResponseResult loadRecommendUsers() {
        // 1.获取用户信息
        TbUser user = AppThreadLocalUtils.getUser();
        List<ApUserRecommand> apUserRecommands = tbUserFanMapper.selectUserFansCount();
        // 判断用户是否存在
        if (user == null) {
            for (ApUserRecommand list : apUserRecommands) {
                TbUser tbUser = tbUserMapper.selectById(list.getUserId());
                list.setUserHead(tbUser.getHeadimg());
                list.setUserName(tbUser.getUsername());
                list.setUserSign(tbUser.getSign());
                // 查询是否被当前用户关注
                // 1.如果用户未登录，则此项全为false
                list.setLike(false);
            }
        } else {
            // 2.如果用户登录，就要查询该用户是否关注推荐用户中的用户
            Long id = user.getId();
            for (ApUserRecommand list : apUserRecommands) {
                TbUser tbUser = tbUserMapper.selectById(list.getUserId());
                list.setUserHead(tbUser.getHeadimg());
                list.setUserName(tbUser.getUsername());
                list.setUserSign(tbUser.getSign());
                // 查询是否被当前用户关注
                TbUserFollow tbUserFollow = tbUserFollowMapper.selectAllByuserIdAndFollowId(id, list.getUserId());
                if(tbUserFollow == null){
                    list.setLike(false);
                }else {
                    list.setLike(true);
                }

            }
        }
        return ResponseResult.okResult(apUserRecommands);
    }

    /**
     * 每月热榜
     *
     * @return
     */
    @Override
    public ResponseResult loadMonthlyHotList() {
        List<ArticleMonthlyHotList> articleMonthlyHotLists = tbShowBehaviorMapper.loadMonthHotList();
        // 将图片、标题、flagid、flagname给每月热榜前三
        for (ArticleMonthlyHotList list : articleMonthlyHotLists) {
            TbArticle tbArticle = tbArticleMapper.selectTitleAndImageAndFlagById(list.getArticleId());
            String[] s = tbArticle.getImage().split(" ");
            list.setImages(s[0]);
            list.setTitle(tbArticle.getTitle());
            list.setFlag(tbArticle.getFlag());
        }
        return ResponseResult.okResult(articleMonthlyHotLists);
    }
}
